# -*- coding:utf-8 -*-
'''eof
name:申请人未结清对外担保五级分类校验
code:Ind_UnpaidCreditClass5Sate_Check
tableName:
columnName:
groups:准入>个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

'''
贷款信息 ICRLoanInfo002*Loan

账户状态 State
五级分类 Class5State

指标值=未结清贷款五级分类 IN ("关注", “次级”, “可疑”, “损失”)笔数
'''


import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')



def Ind_UnpaidCreditClass5Sate_Check():
    null_type_list = ['', None, 'null', '/', 'Null', 'NULL','--','-']
    nostate_type = [u'正常', u'逾期', u'呆账']
    yesstate_type = [u'结清', u'转出']
    state_type = nostate_type+yesstate_type
    count = []
    care_fivestate = [u"关注", u"次级",u"可疑", u"损失"]
    non_fivestate=[u"正常",u"未知"]
    fivestate_type=care_fivestate+non_fivestate
    try:
        if IC in null_type_list:
            return u'缺失值'
        if len(IC) == 0:
            return u'缺失值'
        icr = json.loads(IC[0]["icr"])
        if icr in null_type_list:
            return u'缺失值'
        if len(icr) == 0:
            return u'缺失值'
        if icr.has_key('ICRLoanInfo002*Loan') and len(icr['ICRLoanInfo002*Loan']) >= 1:
            for i in icr['ICRLoanInfo002*Loan']:
                if i.has_key('State') and i.has_key('Class5State'):
                    if i['State'] in nostate_type and i['Class5State'] in care_fivestate:
                        count.append(1)
                    elif i['State'] in state_type and i['Class5State'] in non_fivestate:
                        count.append(0)
                    elif i['State'] in yesstate_type and i['Class5State'] in fivestate_type:
                        count.append(0)
                    else:
                        count.append(None)
                else:
                    count.append(None)
        #若count均为None,则返回缺失值,否则将None置为0
        no_count = filter(lambda x: x is not None, count)
        return sum(no_count) if len(no_count) != 0 else u'缺失值'
    except:
        return u'缺失值'


result = Ind_UnpaidCreditClass5Sate_Check()